<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 27/10/2017
 * Time: 11:46
 */
namespace common\models;
use \common\models\openssl;
date_default_timezone_set("Asia/shanghai");

class Pay
{
    private $params = [];
    public function __construct()
    {
        $this->params = \Yii::$app->params['pay'];
    }

    /**
     * 将参数数组签名
     */
    public static function SignArray($data,$key){
        $data['dog_sk '] = $key;// 将key放到数组中一起进行排序和组装
        krsort($data);
        $blankStr = Pay::ToUrlParams($data);
//        var_dump($data);
//        exit();
        $sign = md5($blankStr);
        return $sign;
    }

    public static function ToUrlParams(array $array)
    {
        $buff = "";
        foreach ($array as $k => $v)
        {
            if($v != "" && !is_array($v)){
//                $buff .= $k . "=" . $v . "&";
                $buff.=$v;
            }
        }

//        $buff = trim($buff, "&");
        return $buff;
    }

    /**
     * 校验签名
     * @param array 参数
     * @param unknown_type appkey
     */
    public static function ValidSign(array $array,$appkey){
        $sign = $array['sign'];
        unset($array['sign']);
        $array['key'] = $appkey;
        $mySign = AppUtil::SignArray($array, $appkey);
        return strtolower($sign) == strtolower($mySign);
    }
}